package com.example.productdataserviceprovider.service.baidu;

import cn.hutool.http.HtmlUtil;
import com.example.productdataserviceprovider.service.AbstractService;
import com.example.productdataserviceprovider.service.search.model.SearchResultModel;
import com.example.productdataserviceprovider.service.utils.HttpUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.util.StringUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Deprecated
public class SearchWordsServiceImpl extends AbstractService implements SearchWordsService {
    private final int perPageSize=10;
    private final Pattern titlePattern=Pattern.compile("<h3 class=\"t\" >(.*?)</h3>");

    private boolean checkCityExist(String title,String keys){
        String[] keyArrays=keys.split(",");
        for(String key:keyArrays){
            if(title.indexOf(key)==-1){
                return false;
            }
        }
        return true;
    }

    @Override
    public List<String> getKeys() {
        List<String> keys=new ArrayList<>();
        keys.add("公共文化场馆,免费开放实施办法");
        keys.add("免费开放专项资金,管理办法");
        keys.add("免费开放专项资金,管理暂行办法");
        keys.add("免费开放以奖代补资金,管理办法");
        return keys;
    }

    @Override
    public List<SearchResultModel> analyse(String keys, int totalPage) {
        List<SearchResultModel> datas=new ArrayList<>();
        String[] keyArrays=keys.split(",");
        StringBuilder wd=new StringBuilder("wd=");
        int index=0;
        for(String key:keyArrays){
            if(index==keyArrays.length-1)
            {
                wd.append(key);
            }else{
                wd.append(key).append("%20");
            }
            index++;
        }
        logger.info("wd.toString={}",wd.toString());
        for(int page=0;page<totalPage;page++){
            int pn=page*perPageSize;
            String baiduUrl="https://www.baidu.com/s?"+wd.toString()+"&pn="+pn;
            String result="";
            try {
                result= HttpUtils.doGet(baiduUrl).replaceAll("(\r\n|\r|\n|\n\r)","");
//                logger.info("result={}",result);
                Matcher matcher=titlePattern.matcher(result);
                while(matcher.find()){
                    String titleHtml=matcher.group(1);

                    Document document = Jsoup.parse(titleHtml);
                    Element element=document.getElementsByTag("a").first();
                    String title= HtmlUtil.cleanHtmlTag(titleHtml).trim();
//                    logger.info("keys={},title={}",keys,title);
                    String url=element.attr("href");

                    boolean flag=checkCityExist(title,keys);
//                    logger.info("title={},keys={},flag={}",title,keys,flag);//TODO
                    if(flag){
                        SearchResultModel model=new SearchResultModel();
                        model.setKey(keys);
                        model.setPage(pn);
                        model.setUrl(baiduUrl);
                        model.setTitle(title);
                        model.setUrl(url);
                        datas.add(model);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(!StringUtils.hasText(result)){
                logger.info("搜索结果为空");
                continue;
            }

        }
        return datas;
    }

    @Override
    public List<SearchResultModel> matcheCity(List<SearchResultModel> datas, Map<String, String> citys) {
        List<SearchResultModel> results=new ArrayList<>();
        for(SearchResultModel data:datas){
            String title=data.getTitle();
            for(Map.Entry<String,String> city:citys.entrySet()){
                if(title.indexOf(city.getKey())!=-1){
                    results.add(data);
                    break;
                }
            }

        }
        return results;
    }

    public void writeToFile(List<SearchResultModel> results,String destFile){
        File file=new File(destFile);
        BufferedWriter writer =null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFile), StandardCharsets.UTF_8));
            for (SearchResultModel data : results) {
                writer.write(data.toString());
                writer.newLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(writer!=null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        CitysServiceImpl citysService=new CitysServiceImpl();
        String cityFile="D:\\library\\library3\\citys.txt";
        Map<String,String> citys=citysService.getCitys(cityFile);
        logger.info("citys.size={}",citys.size());
//        for(Map.Entry<String,String> city:citys.entrySet()){
//            logger.info("city.name={}",city.getKey());
//        }
        int totalPage=1;

        SearchWordsServiceImpl service=new SearchWordsServiceImpl();
        long start=System.currentTimeMillis();
        List<String> keys=service.getKeys();
        CountDownLatch latch=new CountDownLatch(keys.size());
        //根据关键词找出所有的结果，再根据结果去对应的城市匹配，找出来进行生成文件
//        for(String key:keys){
//            new Thread(()->{
//                String destFile="D:/library/library3/result/reults-{key}.txt";
//                List<SearchResultModel> results= service.analyse(key,totalPage);
//                latch.countDown();
//                List<SearchResultModel> matchResults=service.matcheCity(results,citys);
//                logger.info("key={},total.size={},matchResults.size={}",key,results.size(),matchResults.size());
//                destFile=destFile.replaceAll("\\{key\\}",key);
//                service.writeToFile(matchResults,destFile);
//            }).start();
//        }
        //把城市当作一个关键词放进去查询
        for(String key:keys){
            new Thread(()->{
                String destFile="D:/library/library3/result/reults-{key}.txt";
                List<SearchResultModel> results= new ArrayList<>();
                for(Map.Entry<String,String> city:citys.entrySet()){
                    String searchKeys=key+","+city.getKey();
                    List<SearchResultModel> datas= service.analyse(searchKeys,totalPage);
                    results.addAll(datas);
                }
                latch.countDown();
                destFile=destFile.replaceAll("\\{key\\}",key);
                service.writeToFile(results,destFile);
            }).start();
        }
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        logger.info("totalResult:{}"+datas.size());
        long end=System.currentTimeMillis();
        System.out.println("总共花费时间:"+(end-start)/1000);


    }
}
